﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;
using System.IO;

namespace WebHousingInfo
{
    public partial class DienDanBinhLuan : System.Web.UI.Page
    {
        public static string modeid, modedelete, modecate, mode;
        private static int savepage = 0, currentpage, numpage;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                modedelete = Request.QueryString["deletecomment"];
                modeid = Request.QueryString["id"];
                modecate = Request.QueryString["categoryid"];
                mode = Request.QueryString["idsub"];
                if (modedelete != null)
                {
                    ForumCommentBO.DeleteForumCommentBO(Convert.ToInt32(modedelete));
                    Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + modecate + "&id=" + modeid);
                }
                //phan trang
                string linkpaging = "DienDanBinhLuan.aspx?categoryid=" + modecate + "&id=" + modeid + "&";
                numpage = 5;
                string current = null;
                current = Request.QueryString["page"];
                if (current != null)
                    currentpage = int.Parse(current);
                else currentpage = 1;
                int totalrow = gettotalpage();
                int totalpage = totalrow / numpage;
                if (totalrow % numpage != 0)
                    totalpage += 1;
                lblpaging.Text = "";
                if (currentpage > 1)
                {
                    lblpaging.Text += "<ul><li><a href=\"" + linkpaging + "page=1\">◄◄</a></li>";
                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + (currentpage - 1).ToString() + "\">◄</a></li>";
                }
                else
                {
                    lblpaging.Text += "<ul><li><a>◄◄</a></li>";
                    lblpaging.Text += "<li><a>◄</a></li>";
                }
                int custom = 5;
                if (currentpage > custom)
                {
                    if (currentpage > savepage)
                    {
                        if (currentpage + custom <= totalpage)
                        {
                            for (int i = currentpage; i <= currentpage + custom; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                        else
                        {
                            for (int i = totalpage - custom; i <= totalpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                    }
                    else
                    {
                        if (currentpage - custom >= 1)
                        {
                            for (int i = currentpage - custom; i <= currentpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                        else
                        {
                            for (int i = 1; i <= currentpage; i++)
                            {
                                if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                                {
                                    lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                                }
                                else  // Trang khác
                                {
                                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                                }

                            }// end for
                        }
                    }
                }
                else
                {
                    if (custom > totalpage)
                        custom = totalpage;
                    for (int i = 1; i <= custom; i++)
                    {
                        if (i == currentpage)
                        {
                            lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                        }
                        else  // Trang khác
                        {
                            lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                        }

                    }// end for
                }

                if (currentpage < totalpage)
                {
                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + (currentpage + 1).ToString() + "\">►</a></li>";
                    lblpaging.Text += "<li><a href=\"" + linkpaging + "page=" + totalpage.ToString() + "\">►►</a></li></ul>";
                }
                else
                {
                    lblpaging.Text += "<li><a>►</a></li>";
                    lblpaging.Text += "<li><a>►►</a></li></ul>";
                }
                savepage = currentpage;
            }
        }

        protected void btnguitraloinhanh_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
                Response.Redirect("DangNhap.aspx");
            ForumCommentBO forumcommentbo = ForumCommentBO.NewForumCommentBO();
            forumcommentbo.ForumNewID = Convert.ToInt32(modeid);
            forumcommentbo.UserName = Session["UserName"].ToString();
            forumcommentbo.ForumCommentTitle = "";
            forumcommentbo.CommentContent = txttraloinhanh.Text;
            forumcommentbo.CreatedDate = DateTime.Now;
            forumcommentbo.UpdatedDate = DateTime.Now;
            try
            {
                forumcommentbo.Save();
                Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + modecate + "&id=" + modeid);
                btnguitraloinhanh.Focus();
            }
            catch
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã xay ra lỗi khi gửi bài. Xin hãy thử lại !')", true);
                return;
            }
        }

        protected void btntrolai_Click(object sender, EventArgs e)
        {
            Response.Redirect("DienDanTinTheoChuDe.aspx?idsub=" + modecate);
        }

        public static string DeleteQuote(string content)
        {
            try
            {
                content = content.Replace("</blockquote_bydiendanhadat>", "");
                content = content.Replace("<blockquote_bydiendanhadat>", "");
                return content;
            }
            catch
            {
                return content;
            }
        }

        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/UserAvatar/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        public ForumCommentBOList Getdatasource(int forumid)
        {
            ForumCommentBOList forumcommentlist;
            try
            {
                forumcommentlist = ForumCommentBOList.GetForumCommentBOList_ByID(forumid, currentpage, numpage);
                return forumcommentlist;
            }
            catch
            {
                return null;
            }
        }

        public int gettotalpage()
        {
            int total = 0;
            try
            {
                total = ForumCommentBOList.GetForumCommentBOList_ByID(Convert.ToInt32(modeid), -1, -1).Count;
            }
            catch { return 0; }
            return total;
        }
    }
}